﻿//Portions Copyright (C) 2011 Randall Shattuck
//Portions Copyright (C) 2007-2010 Owners of libeveapi 
//    (http://libeveapi.googlecode.com)
//
//EveDashboard.Api was forked from libeveapi 1.1.3
//
//This program is free software; you can redistribute it and/or modify it under
//the terms of the GNU General Public License as published by the Free Software 
//Foundation; either version 2 of the License, or (at your option) any later 
//version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT 
//ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
//FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
//details. You should have received a copy of the GNU General Public License 
//along with this program; if not, write to the Free Software Foundation, Inc.,
//59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;

namespace EveDashboard.Api.ResponseObjects.Parsers
{
    ///<summary> 
    /// A parser which converts a given <see cref="XmlDocument"/> to a <see cref="Research"/>. 
    ///</summary> 
    internal class ResearchResponseParser : IApiResponseParser<Research>
    {
        public Research Parse(XmlDocument xmlDocument)
        {
            this.CheckVersion(xmlDocument);
            Research research = new Research();
            research.ParseCommonElements(xmlDocument);

            List<Research.ResearchItem> researchList = new List<Research.ResearchItem>();
            foreach (XmlNode node in xmlDocument.SelectNodes("//rowset[@name='research']/row"))
            {
                researchList.Add(ParseTransactionRow(node));
            }
            research.ResearchItems = researchList.ToArray();

            return research;
        }

        /// <summary> 
        /// Create a ResearchItem by parsing a single row 
        /// </summary> 
        /// <param name="researchRow">XmlNode containing the ResearchItem data</param> 
        /// <returns></returns> 
        private static Research.ResearchItem ParseTransactionRow(XmlNode researchRow)
        {
            Research.ResearchItem researchItem = new Research.ResearchItem();

            researchItem.AgentID = Convert.ToInt32(researchRow.Attributes["agentID"].InnerText, CultureInfo.InvariantCulture);
            researchItem.SkillTypeID = Convert.ToInt32(researchRow.Attributes["skillTypeID"].InnerText, CultureInfo.InvariantCulture);
            researchItem.ResearchStartDate = TimeUtilities.ConvertCCPTimeStringToDateTimeUTC(researchRow.Attributes["researchStartDate"].InnerText);
            researchItem.ResearchStartDateLocal = TimeUtilities.ConvertCCPToLocalTime(researchItem.ResearchStartDate);
            researchItem.PointsPerDay = Convert.ToDecimal(researchRow.Attributes["pointsPerDay"].InnerText, CultureInfo.InvariantCulture);
            researchItem.RemainderPoints = (float)Convert.ToDouble(researchRow.Attributes["remainderPoints"].InnerText, CultureInfo.InvariantCulture);

            return researchItem;
        }

        /// <summary> 
        /// Compares the object version to the EveApi response version 
        /// </summary> 
        /// <param name="xmlDocument">XML docuemnt to compare version against</param> 
        public void CheckVersion(XmlDocument xmlDocument)
        {
            if (Research.VersionCheck)
            {
                string version = xmlDocument.SelectSingleNode("//eveapi").Attributes["version"].InnerText;
                if (version.CompareTo(Research.API_VERSION) != 0)
                {
                    throw new ApiVersionException(version, Research.API_VERSION);
                }
            }
        }
    }
}
